﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Build.Utilities;
using Microsoft.Web.Publishing.Tasks;
using Microsoft.Build.Framework;
using System.Xml;
using System.IO;

namespace Deployr
{
    public class ConfigTransformation : Task
    {
        [Required]
        public string Configuration
        {
            get;
            set;
        }

        [Required]
        public string SourceFolder
        {
            get;
            set;
        }

        [Required]
        public string DestinationFolder
        {
            get;
            set;
        }

        public override bool Execute()
        {
            XmlTransformation transformation = new XmlTransformation(string.Format(Path.Combine(this.SourceFolder, "Web.{0}.Config"), this.Configuration));

            XmlDocument config = new XmlDocument();
            config.Load(Path.Combine(this.DestinationFolder, "Web.Config"));

            bool result = transformation.Apply(config);
            transformation = null;

            config.Save(Path.Combine(this.DestinationFolder, "Web.Config"));

            return result;
        }
    }
}